﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.RabitsWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.RabitsWay
{
    public class AddRabitsCommandHandler : IRequestHandler<AddRabitsCommand, ApiResult<int>>
    {
        IBaseReponsitory<Rabits> Rabits;
        public AddRabitsCommandHandler(IBaseReponsitory<Rabits> Rabits)
        {
            this.Rabits = Rabits;
        }
        public async Task<ApiResult<int>> Handle(AddRabitsCommand request, CancellationToken cancellationToken)
        {
            if (request.Rabits.SId == 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "商品名称不能为空",
                    Data = -1
                };
            }
            if (request.Rabits.Rabit_Batch == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "批次号不能为空,只能输入30位",
                    Data = -1
                };
            }
            if (request.Rabits.Rabit_Shelflife == 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "保质期不能为空,只能输入5位",
                    Data = -1
                };
            }
            if (request.Rabits.Rabit_Shencan == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "生产许可证不能为空,只能输入30位",
                    Data = -1
                };
            }
            if (request.Rabits.Rabit_Company == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "生产企业不能为空,只能输入30位",
                    Data = -1
                };
            }
            if (request.Rabits.Rabit_TitleImg == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "商品图片不能为空",
                    Data = -1
                };
            }
            request.Rabits.CreateTime = DateTime.Now;

            var Bachlist = Rabits.GetQuery().Where(c => c.Rabit_Batch == request.Rabits.Rabit_Batch && !c.Rabit_IsDel).ToList();
            if (Bachlist.Count > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "该批次号已存在",
                };
            }
            if (request.Rabits.Rabit_Batch == null)
            {
                var Nid = 1;
                var Batch = await Rabits.GetQuery().OrderByDescending(c => c.Rabit_Batch).FirstOrDefaultAsync();
                if (Batch != null)
                {
                    Nid = Convert.ToInt32(Batch.Rabit_Batch.Replace("Bc", ""));
                    Nid++;
                }
                request.Rabits.Rabit_Batch = "Bc" + Nid.ToString().PadLeft(3, '0');
            }

            var list = Rabits.GetQuery().Where(c => c.SId == request.Rabits.SId && !c.Rabit_IsDel).ToList();
            if (list.Count > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "该商品已有溯源",
                    Data = -1
                };
            }
            else
            {
                await Rabits.AddAsync(request.Rabits);
                return new ApiResult<int>
                {
                    Code = ApiEnum.Success,
                    Message = "添加成功",
                    Data = 1
                };
            }
        }
    }
}
